package in.co.codedoc.json.mapper;

import in.co.codedoc.cg.annotations.IsAJSONMapper;
import in.co.codedoc.json.JSONDecoderMethod;
import in.co.codedoc.json.JSONEncoderMethod;
import in.co.codedoc.json.JSONObjectValue;
import in.co.codedoc.time.DayTime;

@IsAJSONMapper
public class DayTimeJSONMapper
{
	@JSONEncoderMethod
	public static JSONObjectValue Encode(DayTime daytime)
	{
		JSONObjectValue json = new JSONObjectValue();
		
		if(daytime != null)
		{
			json.SetProperty("day",DayJSONMapper.Encode(daytime.GetDay()));
			json.SetProperty("time",TimeJSONMapper.Encode(daytime.GetTime()));
		}
		
		return json;
	}
	
	@JSONDecoderMethod
	public static DayTime Decode(JSONObjectValue json)
	{
		return json ==null || !json.HasProperty("day") || !json.HasProperty("time")
		?
			null
		:
			new DayTime
			(
				DayJSONMapper.Decode((JSONObjectValue) json.GetProperty("day")),
				TimeJSONMapper.Decode((JSONObjectValue) json.GetProperty("time"))
			);
	}	
}
